package gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.util.ArrayList;

import javax.swing.JPanel;

import model.CardInterface;
import model.CardRank;
import model.CardSuit;

public class CardContainer extends JPanel
{
	private int containerWidth, containerHeight;
	
	private ArrayList<Card> cards = new ArrayList<Card>();
	
	private State state = State.FULL;
	
	private int margin = 5;
	
	public CardContainer()
	{
		setOpaque(false);
		setLayout(new Layout());
	}
	
	public void setCards(ArrayList<CardInterface> cards)
	{
		removeAll();
		
		this.cards.clear();
		
		if (cards != null)
		{
			for (CardInterface card: cards)
			{
				this.cards.add(new Card(card));
			}
		}
		
		for (Card card: this.cards)
		{
			add(card);
		}
		
		// Create a new layout for the card container.
		revalidate();
	}
	
	public void setTestCards()
	{
		ArrayList<CardInterface> cards = new ArrayList<CardInterface>();
		
		cards.add(model.Card.valueOf(CardRank.QUEEN, CardSuit.HEARTS));
		cards.add(model.Card.valueOf(CardRank.QUEEN, CardSuit.HEARTS));
		
		setCards(cards);
	}
	
	private int getPixelsBetweenCards(int cardWidth)
	{
		int pixelsBetweenCards = 0;
		
		switch (state)
		{
			case PARTIAL:
				
				pixelsBetweenCards = (int) Math.round(cardWidth * 0.25);
				
				break;
			
			case FULL:
				
				// Card with + padding.
				pixelsBetweenCards = cardWidth + margin;
				
				break;
		}
		
		return pixelsBetweenCards;
	}
	
	public Dimension getPreferredSize()
	{
		int cardsSize = cards.size();
		
		if (cardsSize == 0)
		{
			return new Dimension(0, 0);
		}
		
		int cardsLeft = cardsSize;
		
		if (cardsLeft > 1)
		{
			cardsLeft--;
		}
		
		Card firstCard = cards.get(0);
		
		int cardWidth = firstCard.getWidth();
		
		Dimension d = new Dimension();
		d.width = cardsLeft * getPixelsBetweenCards(cardWidth) + cardWidth + 2 * margin;
		d.height = firstCard.getHeight() + 2 * margin;
		
		return d;
	}
	
	public void setState(State state)
	{
		
	}
	
	public enum State
	{
		PARTIAL, FULL;
	}
	
	private class Layout implements LayoutManager
	{
		public void addLayoutComponent(String name, Component comp)
		{
			
		}

		public void layoutContainer(Container parent)
		{
			int cardsSize = cards.size();
			
			for (int i = 0; i < cardsSize; i++)
			{
				Card card = cards.get(i);
				
				int pixelsLeft = i * getPixelsBetweenCards(card.getWidth());
				Dimension d = card.getPreferredSize();
				card.setBounds(pixelsLeft + margin, margin, d.width, d.height);
				//setComponentZOrder(card, cardsSize - 1 - i);
			}
		}

		public Dimension minimumLayoutSize(Container parent)
		{
			return preferredLayoutSize(parent);
		}

		public Dimension preferredLayoutSize(Container parent)
		{
			return new Dimension(parent.getWidth(), parent.getHeight());
		}

		public void removeLayoutComponent(Component comp)
		{
			
		}
	}
}
